<template>
   <div id="home" class="wrapper">
     <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
     <tab-control class="tab-control"
                  ref="tabControl1"
                  @tabClick="tabClick"
                  :titles="['流行', '新款', '精选']"
                  v-show="isTabFixed"/>

        <scroll class="content"
                ref="scroll"
                :probe-type="3"
                :pull-up-load="true"
                @scroll="contentScroll"
                @pullingUp="loadMore">
          <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
          <recommend-view :recommends="recommends"/>
          <feature-view/>
          <tab-control ref="tabControl2"
                       @tabClick="tabClick"
                       :titles="['流行', '新款', '精选']"/>
          <goods-list :goodsList="showGoodsList"/>
        </scroll>
        <back-top @click.native="backClick" v-show="showBackTop"/>
   </div>
</template>

<script>

import HomeSwiper from './childComps/HomeSwiper'
import RecommendView from './childComps/RecommendView'
import FeatureView from './childComps/FeatureView'

import NavBar from 'components/common/navbar/NavBar'
import TabControl from 'components/content/tabControl/TabControl'
import GoodsList from 'components/content/goods/GoodsList'
import BackTop from 'components/content/backTop/BackTop'
import Scroll from 'components/common/scroll/Scroll'

import {debounce} from 'common/utils'
import {getHomeMultiData, getHomeData, BANNER, RECOMMEND} from 'network/home'
import {itemListenerMixin} from 'common/mixin'
import {POP, NEW, SELL, BACKTOP_DISTANCE} from 'common/const'

export default {
  data() {
    return {
      banners: [],
      recommends: [],
      goodsList: {
        'pop': {page: 1, list: []},
        'new': {page: 1, list: []},
        'sell': {page: 1, list: []}
      },
      saveY: 0,
      isTabFixed: false,
      showBackTop: false,
      tabOffsetTop: 0,
      currentType: POP 
    };
  },
  mixins: [itemListenerMixin],
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    NavBar,
    GoodsList,
    BackTop,
    Scroll
  },
  created() {
    console.log("创建Home");
    this.getMultiData()
    this.getHomeProducts(POP)
    this.getHomeProducts(NEW)
    this.getHomeProducts(SELL)
  },
  destroyed() {
    console.log("home destroyed");
  },
  activated() {
    this.$refs.scroll.refresh()
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
  },
  computed:{
    showGoodsList() {
      return this.goodsList[this.currentType].list
    }
  },

  mounted () {
    const refresh = debounce(this.$refs.scroll.refresh, 50)
    this.$bus.$on('itemImageLoad', () => {
      refresh()
    })
  },

  methods: {
    tabClick(index) {
      switch(index) {
        case 0:
          this.currentType = POP
          break
        case 1:
          this.currentType = NEW
          break
        case 2:
          this.currentType = SELL
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index 
    },

    backClick() {
      this.$refs.scroll.scrollTo(0, 0)
    },

    loadMore() {
      this.getHomeProducts(this.currentType)
    },

    contentScroll(position) {
      this.showBackTop = position.y < -BACKTOP_DISTANCE
      this.isTabFixed = position.y < -this.tabOffsetTop
    },

    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop 
    },

    getMultiData() {
      getHomeMultiData().then(res => {
        this.banners = res.data[BANNER].list
        this.recommends = res.data[RECOMMEND].list
      })
    },

    getHomeProducts(type) {
      getHomeData(type, this.goodsList[type].page).then(res => {
        const goodsList = res.data.list
        this.goodsList[type].list.push(...goodsList)
        this.goodsList[type].page += 1

        this.$refs.scroll.finishPullUp()
      })
    }
  },
}
</script>

<style scoped>
  #home {
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;
  }

  .content {
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }
</style>